package net.begincode.service.blog.impl;

import java.util.ArrayList;
import java.util.List;

import config.DbTemplate;
import net.begincode.bean.BcBlog;
import net.begincode.bean.BcClassify;
import net.begincode.service.blog.ClassifyService;
import net.begincode.service.user.impl.BcUserServiceImpl;

public class ClassifyServiceImpl implements ClassifyService{
	
	private ClassifyServiceImpl() {
	}

	private static ClassifyServiceImpl instance = null;

	public static ClassifyServiceImpl getInstance() {
		if (instance == null) {
			synchronized (ClassifyServiceImpl.class) {
				if (instance == null) {
					instance = new ClassifyServiceImpl();
				}
			}
		}
		return instance;
	}
	
	@Override
	public List<BcClassify> selClassifyByUserId(String bcUserId) {
		// TODO Auto-generated method stub
		String selSql =" select * from bc_classify where bc_user_id = ?  and sts = 1 ";
		return DbTemplate.queryForList(selSql, new Object[]{bcUserId}, BcClassify.class);
	}

    @Override
    public BcClassify addClassify(BcClassify bcClassify) {
        StringBuilder addClassify = new StringBuilder();
        addClassify.append(" insert into bc_classify (bc_user_id,classify_note,check_flag,sts,cdate) ");
        addClassify.append(" values (?, ?, '0','1',now()) ");
        int returnValue = DbTemplate.insert(addClassify.toString(),
                new Object[] {
                    bcClassify.getBc_user_id(),
                    bcClassify.getClassify_note()
                });
        if(returnValue > 0){
            String selSql = " select * from bc_classify where sts = '1' and bc_user_id = ? order by bc_classify_id desc ";
            return DbTemplate.queryForObject(selSql, new Object[]{bcClassify.getBc_user_id()}, BcClassify.class);
        }else{
            return null;
        }
    }

    @Override
    public int updClassify(BcClassify bcClassify) {
        ArrayList<Object> paraList = new ArrayList<Object>();
        StringBuilder updSql = new StringBuilder();
        updSql.append(" update bc_classify set classify_note = ?, check_flag  = ? where bc_classify_id = ? ");
        paraList.add(bcClassify.getClassify_note());
        paraList.add(bcClassify.getCheck_flag());
        paraList.add(bcClassify.getBc_classify_id());
        return DbTemplate.update(updSql.toString(), paraList.toArray());
    }

    @Override
    public int delClassify(int bc_classify_id) {
        String selSql = " select * from bc_blog where bc_classify_id = ? ";
        List<BcBlog> bcClassifies = DbTemplate.queryForList(selSql, new Object[]{bc_classify_id}, BcBlog.class);
        if (bcClassifies.size() > 0) {
            return 0;
        } else {
            ArrayList<Object> paraList = new ArrayList<Object>();
            StringBuilder updSql = new StringBuilder();
            updSql.append(" update bc_classify set sts = ? where bc_classify_id = ? ");
            paraList.add(0);
            paraList.add(bc_classify_id);
            return DbTemplate.update(updSql.toString(), paraList.toArray());
        }
    }

	@Override
	public int addDefaultClassify(int userId) {
		// TODO Auto-generated method stub
        StringBuilder addClassify = new StringBuilder();
        addClassify.append(" insert into bc_classify (bc_user_id,classify_note,check_flag,sts,cdate) ");
        addClassify.append(" values (?, ?, '0','1',now()) ");
        return DbTemplate.insert(addClassify.toString(),  new Object[] {userId, "默认分类"});

	}

}
